/*
 * Copyright 2010 Dovado FZ-LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dovado.dovadoremote;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class About extends Activity {
	String[] labels;

	/*
	 * The "spinner" object. This pops up and displays different
	 * "about"-categories
	 */
	Spinner s1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.about_main);

		/*
		 * Display the wonderful logo:
		 */
		ImageView i = (ImageView) findViewById(R.id.dovadologo);
		i.setImageResource(R.drawable.logo);

		/*
		 * Array of labels. These will populate the spinner
		 */
		final String[] labels = {
				getResources().getString(R.string.howto_router),
				getResources().getString(R.string.howto_app),
				getResources().getString(R.string.general),
				getResources().getString(R.string.licensing) };

		/*
		 * Give our spinner the properties from spinner in XML-file
		 */
		s1 = (Spinner) findViewById(R.id.spinner1);

		/*
		 * The ArrayAdapter holds the actual data for the spinner
		 */
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, labels);

		s1.setAdapter(adapter);

		/*
		 * When we select a topic in the spinner the body-text will be displayed
		 * in a Webview (which is easy to format).
		 */
		s1.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				int index = s1.getSelectedItemPosition();
				WebView textField = (WebView) findViewById(R.id.about_bodytext);
				switch (index) {
				case 2:
					textField.loadDataWithBaseURL(null,
							getString(R.string.about_text_router_general),
							"text/html", "utf-8", null);
					break;
				case 0:
					textField.loadDataWithBaseURL(null,
							getString(R.string.about_text_router_config),
							"text/html", "utf-8", null);
					break;
				case 1:
					textField.loadDataWithBaseURL(null,
							getString(R.string.about_text_client_config),
							"text/html", "utf-8", null);
					break;
				case 3:
					textField.loadDataWithBaseURL(null,
							getString(R.string.about_text_router_licensing),
							"text/html", "utf-8", null);
					break;
				default:
					textField.loadDataWithBaseURL(null,
							getString(R.string.about_text_router_general),
							"text/html", "utf-8", null);
				}

			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

	}

	/*
	 * If the user presses the 'back'-button (we can add some more functionality
	 * here
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent i = new Intent();
			i.setAction(Intent.ACTION_MAIN);
			i.addCategory(Intent.CATEGORY_HOME);
			this.startActivity(i);
			return true;
		}
		return false;
	}

}
